package yxy.game.pm2.command.yilingbattle;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.YiLingMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.yiling.YiLingBattleHandle;
import yxy.game.pm2.module.yiling.YiLingBattleModule;

/****************************************
 * @name CMD_YiLingBattleSweepBuy
 * @description 修炼塔 挑战次数购买
 * @author 梁健彬
 * @since 2022-05-17
 ***************************************/
@MessageType(proto = Command.YiLingBattleSweepBuy, classtype = YiLingMessage.YiLingBattleSweepBuyRequest.class)
public class CMD_YiLingBattleSweepBuy extends Command<YiLingMessage.YiLingBattleSweepBuyRequest> {

    @Override
    public void execute(MessageSource messageSource, YiLingMessage.YiLingBattleSweepBuyRequest request) {

        String playerCode = messageSource.getTag();

        int times = request.getTimes();

        YiLingMessage.YiLingBattleSweepBuyResponse.Builder response = YiLingMessage.YiLingBattleSweepBuyResponse.newBuilder();

        YiLingBattleModule module = YiLingBattleModule.get(playerCode);

        try {
            if (module.tryLock()) {

                int cost = YiLingBattleHandle.buytimescost(times);

                if (times <= 0) {
                    send(messageSource, response, Result.ParamError);
                    return;
                }

                if (module.remainBuyTimes() < times) {
                    send(messageSource, response, Result.YiLingBattleBuyTimesLack);// 已经没有购买次数了
                    return;
                }

                try {
                    new CurrencyHandle().cost(playerCode, CCReason.夷陵之战, Currency.create(CurrencyType.CT_元宝, 0, cost));
                } catch (CurrencyNotEnoughException e) {
                    send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
                    return;
                }

                module.setBuytimes(module.getBuytimes() + times);
                module.set();

				response.setTimes(module.remainTimes());
				response.setBuytimes(module.remainBuyTimes());
				response.setBuycost(cost);

				send(messageSource, response, Result.Success);
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);

        } finally {
            module.unlock();
        }
    }

    private void send(MessageSource messageSource, YiLingMessage.YiLingBattleSweepBuyResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}